﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Library
{
    public abstract class CommonService
    {
        protected readonly List<User> Users = new() { new("fzy", "12345", RoleConstants.ADMIN) };
        protected readonly Dictionary<string, Book> Books = new() {
                    { "Java基础编程", new("Java基础编程", "余胜军", 100) },
                    {"Python基础编程", new("Python基础编程", "金角大王", 200) }
        };

        public virtual (bool, User?) JudgeAdminConsole()
        {
            Console.Write("请输入管理员账号: ");
            var username = Console.ReadLine();
            Console.Write("请输入管理员密码: ");
            var password = Console.ReadLine();
            return Verification(username, password, RoleConstants.ADMIN);
        }

        protected virtual (bool, User?) Verification(string? username, string? password, int role = RoleConstants.USER)
        {
            var existingUser = Users.Find(user => user.UserName == username);

            if (existingUser == null)
            {
                Console.WriteLine($"未找到用户 {username}!");
                return (false, null);
            }

            if (existingUser.Password != password)
            {
                Console.WriteLine("密码错误!");
                return (false, null);
            }

            if (existingUser.Role != RoleConstants.ADMIN && role == RoleConstants.ADMIN)
            {
                Console.WriteLine("权限不足!");
                return (false, null);
            }

            return (true, existingUser);
        }

        public virtual (bool, User?) JudgeUserConsole()
        {
            Console.Write("请输入用户账号: ");
            var username = Console.ReadLine();
            Console.Write("请输入用户密码: ");
            var password = Console.ReadLine();
            return Verification(username, password);
        }

        protected virtual bool UserBorrowOrReturnBook(int Mode, User user, string title, int quantity)
        {
            if (Mode == OperatorConstants.BORROWBOOK) return SyncBorrowBook(user, title, quantity);
            else if (Mode == OperatorConstants.RETURNBOOK) return SyncReturnBook(user, title, quantity);
            return false;
        }

        protected virtual bool SyncBorrowBook(User user, string title, int quantity)
        {
            user.BorrowBook(title, quantity);
            return true;
        }

        protected virtual bool SyncReturnBook(User user, string title, int quantity)
        {
            return user.ReturnBook(title, quantity);
        }

        protected virtual void LoadBooksFromCSV(string path)
        {
            if (!File.Exists(path))
            {
                Console.WriteLine($"{path} 目标路径文件不存在");
                return;
            }

            var lines = File.ReadAllLines(path);

            for (int i = 1; i < lines.Length; i++)
            {
                var line = lines[i];
                var values = line.Split(',');
                if (values.Length != 3)
                {
                    Console.WriteLine($"{path} 目标路径文件信息有误!");
                    return;
                }
                var title = values[0].Trim();
                var author = values[1].Trim();
                var quantity = int.Parse(values[2].Trim());

                var book = new Book(title, author, quantity);
                Books[title] = book;
            }
        }
    }
}
